//
//  QryResultViewController.m
//  SelfDriving
//
//  Created by apple on 16/6/30.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "QryResultViewController.h"
#import "BusinessService+FJ.h"
#import "FJModel.h"
#import "FriendDetailsMainVC.h"
#import "MemberRoamingCell.h"
@interface QryResultViewController ()<BaseCollectionDelegate>
@property (weak, nonatomic) IBOutlet BaseCollection *collection;

@end

@implementation QryResultViewController
{
    NSMutableArray *data;
    NSMutableArray *datas;
    UILabel *notlabel;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    _collection.bdelegate = self;
    datas = [[NSMutableArray alloc]init];
    data = [[NSMutableArray alloc]init];
}
-(void)initData:(int)index{
         NSMutableDictionary *dic = [UserDefaults readUserDefaultObjectValueForKey:@"userLocation"];
        [dic addEntriesFromDictionary:_dic];
        [dic setObject:[NSNumber numberWithInt:index] forKey:@"page"];
        [[BusinessService shareInstance]nearby:dic success:^(id result) {
            //         datas = [_collection result:result olddata:datas index:index];
            datas = result;
            LoginResponseModel *model = [datas firstObject];
            if (index == 1) {
                [data removeAllObjects];
            }
            [data addObjectsFromArray:model.personlist];
            if (notlabel) {
                [notlabel removeFromSuperview];
            }
            notlabel = (UILabel *)[_collection notData];
            if (data.count == 0) {
                [self.view addSubview:notlabel];
            }
            [_collection reloadData];
         } failure:^(id result) {
            [SVProgressHUD showErrorWithStatus:result];
        }];
}
#pragma mark collectionViewDelegate

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)sectio{
    return data.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    MemberRoamingCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MemberRoamingCell" forIndexPath:indexPath];
    NSMutableDictionary *dic = [data objectAtIndex:indexPath.row];
    LoginResponseModel *model = [[LoginResponseModel alloc]init];
    [model serializationWithDictionary:dic];
    
    cell.name_label.text = model.nickname;
    [ValidateHelper setImage:model.headsmall img:cell.head_img istoux:NO];
    NSString *age = [NSString stringWithFormat:@"%@岁",[ValidateHelper timeToAge:model.age]];
    NSString *job = model.job;
    NSString *distance = [ValidateHelper DistanceFormat:model.distance];
    cell.sign_lable.text = [NSString stringWithFormat:@"%@ %@ %@",age,job,distance];
    if([model.memberlevel intValue]!=0){
        cell.vip_img.hidden = NO;
    }else{
        cell.vip_img.hidden = YES;
    }
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    CGSize size = CGSizeMake((SCRE_WIDTH-30)/2, (SCRE_WIDTH-30)/2);
    return size;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    NSMutableDictionary *dic = [data objectAtIndex:indexPath.row];
    LoginResponseModel *model = [[LoginResponseModel alloc]init];
    [model serializationWithDictionary:dic];
    
    DTModel *mode = [[DTModel alloc]init];
    mode.uid = model.uid;
    UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"MS"
                                                                 bundle:[NSBundle mainBundle]];
    FriendDetailsMainVC *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"FriendDetailsMainVC"];
    searchResultDetailController.model = mode;
    [self.navigationController pushViewController:searchResultDetailController animated:YES];
}

- (IBAction)backAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
